package com.ck.express.activity

import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Typeface
import android.os.Bundle
import android.text.InputType
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.ck.express.R
import com.ck.express.databinding.ActivityMenuBinding
import com.ck.express.tcp.TcpClient

class MenuActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMenuBinding


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMenuBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupUI()
    }

    private fun setupUI() {
        // 设置工具栏
        binding.toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.primary))
        binding.toolbar.title = "物流操作系统 - ${LogisticsApp.userSession.getOrgName()}"

        // 设置用户和组织信息
        binding.userInfoText.text = "用户: ${LogisticsApp.userSession.getUserName()}"
        binding.orgInfoText.text = "组织: ${LogisticsApp.userSession.getOrgName()}"

        // 供件按钮点击事件
        binding.deliveryButton.setOnClickListener {
//            scanActivity.launchExternalScanner()
            startActivity(Intent(this, ScanActivity::class.java))
        }

        // 轨迹查询按钮点击事件
       /* binding.trackingButton.setOnClickListener {
            Toast.makeText(this, "轨迹查询功能", Toast.LENGTH_SHORT).show()
        }*/

        // 操作记录按钮点击事件
        binding.historyButton.setOnClickListener {
            Toast.makeText(this, "操作记录功能", Toast.LENGTH_SHORT).show()
            startActivity(Intent(this,OperationRecordActivity::class.java))
        }

      /*  // 系统设置按钮点击事件
        binding.settingsButton.setOnClickListener {
            showIpConfigDialog()
        }*/

        // 切换账户按钮点击事件
        binding.logoutButton.setOnClickListener {
            LogisticsApp.userSession.clearSession()
            startActivity(Intent(this, LoginActivity::class.java))
            finish()
        }

        // 退出系统按钮点击事件
        binding.exitButton.setOnClickListener {
            AlertDialog.Builder(this)
                .setTitle("确认退出")
                .setMessage("确定要退出系统吗？")
                .setPositiveButton("确定") { _, _ -> finishAffinity() }
                .setNegativeButton("取消", null)
                .show()
        }
    }

   /* private fun showIpConfigDialog() {
        val layout = LinearLayout(this).apply {
            orientation = LinearLayout.VERTICAL
            setPadding(32, 16, 32, 16)
        }

        val prefs = getSharedPreferences("ip_config", Context.MODE_PRIVATE)

        // 登录服务器配置
        val loginIpEditText = createIpEditText("登录服务器IP", prefs.getString(KEY_LOGIN_IP, ""))
        val loginPortEditText =
            createPortEditText("登录服务器端口", prefs.getString(KEY_LOGIN_PORT, ""))

        // 下车服务器配置
        val unloadIpEditText = createIpEditText("下车服务器IP", prefs.getString(KEY_UNLOAD_IP, ""))
        val unloadPortEditText =
            createPortEditText("下车服务器端口", prefs.getString(KEY_UNLOAD_PORT, ""))

        // 落格服务器配置
        val sortIpEditText = createIpEditText("落格服务器IP", prefs.getString(KEY_SORT_IP, ""))
        val sortPortEditText =
            createPortEditText("落格服务器端口", prefs.getString(KEY_SORT_PORT, ""))

        addSection(layout, "登录服务器配置")
        layout.addView(loginIpEditText)
        layout.addView(loginPortEditText)

        addSection(layout, "下车服务器配置")
        layout.addView(unloadIpEditText)
        layout.addView(unloadPortEditText)

        addSection(layout, "落格服务器配置")
        layout.addView(sortIpEditText)
        layout.addView(sortPortEditText)

        AlertDialog.Builder(this)
            .setTitle("服务器配置")
            .setView(layout)
            .setPositiveButton("保存") { _, _ ->
                prefs.edit().apply {
                    putString(KEY_LOGIN_IP, loginIpEditText.text.toString())
                    putString(KEY_LOGIN_PORT, loginPortEditText.text.toString())
                    putString(KEY_UNLOAD_IP, unloadIpEditText.text.toString())
                    putString(KEY_UNLOAD_PORT, unloadPortEditText.text.toString())
                    putString(KEY_SORT_IP, sortIpEditText.text.toString())
                    putString(KEY_SORT_PORT, sortPortEditText.text.toString())
                    apply()
                }
                TcpClient.configure(
                    loginIpEditText.text.toString(),
                    loginPortEditText.text.toString().toIntOrNull() ?: 10001
                )
                Toast.makeText(this, "配置已保存", Toast.LENGTH_SHORT).show()
            }
            .setNegativeButton("取消", null)
            .show()
    }*/

  /*  private fun createIpEditText(hint: String, defaultValue: String?): EditText {
        return EditText(this).apply {
            this.hint = hint
            setText(defaultValue)
            inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_URI
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                bottomMargin = 16
            }
        }
    }

    private fun createPortEditText(hint: String, defaultValue: String?): EditText {
        return EditText(this).apply {
            this.hint = hint
            setText(defaultValue)
            inputType = InputType.TYPE_CLASS_NUMBER
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                bottomMargin = 16
            }
        }
    }

    private fun addSection(layout: LinearLayout, title: String) {
        val textView = TextView(this).apply {
            text = title
            textSize = 16f
            setTextColor(ContextCompat.getColor(this@MenuActivity, R.color.primary_dark))
            setTypeface(null, Typeface.BOLD)
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                topMargin = 24
                bottomMargin = 8
            }
        }
        layout.addView(textView)
    }*/
}